/**
 * 
 */
package org.compiere.report;

import java.io.File;
import java.io.FileOutputStream;
import java.math.BigDecimal;
import java.util.Properties;
import java.util.logging.Level;

import org.adempiere.exceptions.AdempiereException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.compiere.model.MAssetTransaction;
import org.compiere.model.MAssetTransactionLine;
import org.compiere.model.MAttachmentEntry;
import org.compiere.model.MBPartner;
import org.compiere.model.MOrg;
import org.compiere.process.ProcessInfo;
import org.compiere.process.ProcessInfoParameter;
import org.compiere.process.SvrProcess;
import org.compiere.util.Env;
import org.compiere.util.Msg;

import extend.org.compiere.utils.Util;

/**
 * @author A.Kulantayev
 * 
 */
public class A_Asset_Transaction_Form_DA3 extends SvrProcess {
	//A_Asset_Transaction_Form_DA3
	/** Current context */
	private Properties m_ctx;
	private ProcessInfo pi;
	/**Current Record_ID */
	int p_A_Asset_Transaction_ID = 0;
	/**Out file */
	String ResultPath;
	@Override
	protected void prepare() {
		log.info("");
		m_ctx = Env.getCtx();
		pi = getProcessInfo();
		ProcessInfoParameter[] para = getParameter();
		for (int i = 0; i < para.length; i++) {
			String name = para[i].getParameterName();
			if (para[i].getParameter() == null)
				;
			else if (name.equals("A_Asset_Transaction_ID")) 
				p_A_Asset_Transaction_ID  = para[i].getParameterAsInt();
			else
				log.log(Level.SEVERE, "Unknown Parameter: " + name);
		}
	}

	@Override
	protected String doIt() throws Exception {
		//File path
		ResultPath = Util.getResultPath("A_Asset_Transaction_Form_DA3", 29);
		// 
		  MAttachmentEntry entry = Util. getAttachment(pi, m_ctx, ResultPath);
		if (entry == null)
			throw new AdempiereException(Msg.translate(m_ctx,"NotFoundTemplate"));

		XSSFWorkbook wb = new XSSFWorkbook(entry.getInputStream());
		XSSFSheet sheet = wb.getSheetAt(0);
		Row R = null;
		Cell C = null;
		//Report Header
		MBPartner ad_Org_BP = new MBPartner(m_ctx, new MOrg(m_ctx, Env.getAD_Org_ID(m_ctx), get_TrxName()).getLinkedC_BPartner_ID(get_TrxName()), get_TrxName());
		R = sheet.getRow(7);
		R.getCell(26).setCellValue(new MOrg(m_ctx, Env.getAD_Org_ID(m_ctx), get_TrxName()).getName());
		R.getCell(42).setCellValue(ad_Org_BP.getFI_BIN_IIN());
		R = sheet.getRow(17);
		MAssetTransaction assetTransaction = new MAssetTransaction(m_ctx, p_A_Asset_Transaction_ID, get_TrxName());
		R.getCell(32).setCellValue(assetTransaction.getDocumentNo());
		R.getCell(40).setCellValue(assetTransaction.getDateAcct());
		//
		MAssetTransactionLine[] assetTransactionLines = assetTransaction.getLines(false);
		int rowNo = 25;
		BigDecimal balanceCost = new BigDecimal(0);
		for(int i = 0, nn = 1; i< assetTransactionLines.length; i++,nn++,rowNo++)
		{
			sheet.shiftRows(rowNo, rowNo+55, 1);
			R = sheet.createRow(rowNo);
			for(int j = 0; j < 48; j++)
			{
				R.createCell(j);
				R.getCell(j).setCellStyle(sheet.getRow(1).getCell(0).getCellStyle());
			}
			//set Row height
			int l = 0;
			int diff = 1;
			if(assetTransactionLines[i].getA_Asset().getName().length()>30)
				l = assetTransactionLines[i].getA_Asset().getName().length();
			else
				l = 30;
			if(l>30)
				diff = l/30;
			if(diff>1){
				int heigth = sheet.getRow(0).getHeight()*diff;
				R.setHeight((short)heigth);
			}
//			l = assetTransactionLines[i].getA_Asset().getName().length()/17 == 0? 1:assetTransactionLines[i].getA_Asset().getName().length()/17;
//			R.setHeight((short) (l*sheet.getRow(0).getHeight()));
			//			
			//col1
			R.getCell(0).setCellStyle(sheet.getRow(1).getCell(0).getCellStyle());
			sheet.addMergedRegion(new CellRangeAddress(rowNo, rowNo, 0, 2));
			R.getCell(0).setCellValue(Integer.valueOf(nn));
			//col2
			R.getCell(3).setCellStyle(sheet.getRow(2).getCell(0).getCellStyle());
			sheet.addMergedRegion(new CellRangeAddress(rowNo, rowNo, 3, 12));
			R.getCell(3).setCellValue(assetTransactionLines[i].getA_Asset().getName());
			//col3
			R.getCell(13).setCellStyle(sheet.getRow(17).getCell(40).getCellStyle());
			sheet.addMergedRegion(new CellRangeAddress(rowNo, rowNo, 13, 16));
			R.getCell(13).setCellValue(assetTransactionLines[i].getA_Asset().getAssetServiceDate());
			//col4
			R.getCell(17).setCellStyle(sheet.getRow(17).getCell(40).getCellStyle());
			sheet.addMergedRegion(new CellRangeAddress(rowNo, rowNo, 17, 20));
			R.getCell(17).setCellValue(assetTransactionLines[i].getA_Asset().getAssetServiceDate());
			//col5
			R.getCell(21).setCellStyle(sheet.getRow(1).getCell(0).getCellStyle());
			sheet.addMergedRegion(new CellRangeAddress(rowNo, rowNo, 21, 24));
			R.getCell(21).setCellValue(assetTransactionLines[i].getA_Asset().getUseLifeMonths());
			//col6
			R.getCell(25).setCellStyle(sheet.getRow(0).getCell(0).getCellStyle());
			sheet.addMergedRegion(new CellRangeAddress(rowNo, rowNo, 25, 29));
			R.getCell(25).setCellValue(assetTransactionLines[i].getAssetAmt().negate().doubleValue());
			//col7
			R.getCell(30).setCellStyle(sheet.getRow(0).getCell(0).getCellStyle());
			sheet.addMergedRegion(new CellRangeAddress(rowNo, rowNo, 30, 34));
			R.getCell(30).setCellValue(assetTransactionLines[i].getDeprAmt().negate().doubleValue());
			//col8
			balanceCost = balanceCost.add(assetTransactionLines[i].getAssetAmt().negate().add(assetTransactionLines[i].getDeprAmt()));
			R.getCell(35).setCellStyle(sheet.getRow(0).getCell(0).getCellStyle());
			sheet.addMergedRegion(new CellRangeAddress(rowNo, rowNo, 35, 39));
			R.getCell(35).setCellValue(assetTransactionLines[i].getAssetAmt().negate().add(assetTransactionLines[i].getDeprAmt()).doubleValue());
			//col9
			R.getCell(40).setCellStyle(sheet.getRow(2).getCell(0).getCellStyle());
			sheet.addMergedRegion(new CellRangeAddress(rowNo, rowNo, 40, 43));
			R.getCell(40).setCellValue(assetTransactionLines[i].getA_Asset().getSerNo()==null?" ":assetTransactionLines[i].getA_Asset().getSerNo());
			//col10
			R.getCell(44).setCellStyle(sheet.getRow(1).getCell(0).getCellStyle());
			sheet.addMergedRegion(new CellRangeAddress(rowNo, rowNo, 44, 47));
			R.getCell(44).setCellValue(assetTransactionLines[i].getA_Asset().getValue());
		}
		//Total cells
		R = sheet.getRow(rowNo);
		R.getCell(35).setCellStyle(sheet.getRow(0).getCell(0).getCellStyle());
		R.getCell(35).setCellValue(balanceCost.doubleValue());
		//
		
		//Delete temp cells styles
		CellStyle cellStyle = wb.createCellStyle();
		cellStyle.cloneStyleFrom(sheet.getRow(0).getCell(47).getCellStyle());
		
		R = sheet.getRow(0);
		R.getCell(0).setCellStyle(cellStyle);
		R.getCell(1).setCellValue(" ");
		
		R = sheet.getRow(1);
		R.getCell(0).setCellStyle(cellStyle);
		R.getCell(1).setCellValue(" ");
		
		R = sheet.getRow(2);
		R.getCell(0).setCellStyle(cellStyle);
		R.getCell(1).setCellValue(" ");
		//
		
		// Write the output to a file
		FileOutputStream fileOut = new FileOutputStream(ResultPath);
		wb.write(fileOut);
		fileOut.close();
		// Open the file with the application associated
		Util.launchFile(new File(ResultPath));	
		return Msg.translate(m_ctx, "Success");
	}

}
